package com.qdsgjsfk.vision.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleNotifyCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.jaeger.library.StatusBarUtil;
import com.qdsgjsfk.vision.R;

import java.util.Arrays;


public class BleActivity extends AppCompatActivity {

    Button bClear;
    TextView tvByte;
    TextView tvContent;

//    public static final String UUID_SERVICE = "0000ffe0-0000-1000-8000-00805f9b34fb";
//    public static final String UUID_READ = "0000ffe1-0000-1000-8000-00805f9b34fb";

    public static final String UUID_SERVICE = "0003cdd0-0000-1000-8000-00805f9b0131";
    public static final String UUID_READ = "0003cdd1-0000-1000-8000-00805f9b0131";
    private BleDevice bleDevice;

    private String mData = "";
    private int totalPacket;

    public static void launcher(Context context, BleDevice bleDevice) {
        context.startActivity(new Intent(context, BleActivity.class).putExtra("ble", bleDevice));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_ble);
        StatusBarUtil.setTransparent(this);

        bClear = findViewById(R.id.b_clear);
        tvByte = findViewById(R.id.tv_byte);
        tvContent = findViewById(R.id.tv_content);
        bleDevice = getIntent().getParcelableExtra("ble");
        if (bleDevice == null) {
            finish();
        }

        refreshText();

        initBle();

        initListener();
    }

    private void initListener() {
        bClear.setOnClickListener(v -> {
            mData = "";
            totalPacket = 0;
            refreshText();
        });
    }

    private void initBle() {
        BleManager.getInstance().notify(
                bleDevice,
                UUID_SERVICE,
                UUID_READ,
                new BleNotifyCallback() {

                    @Override
                    public void onNotifySuccess() {
                        Log.i("TAG", "onNotifySuccess");
                    }

                    @Override
                    public void onNotifyFailure(final BleException exception) {
                        Log.i("TAG", "onNotifyFailure");
                    }

                    @Override
                    public void onCharacteristicChanged(byte[] data) {
                        Log.e("TAG", "**************:" + Arrays.toString(data));
                        String bleDataTemp = new String(data).trim();

                        mData += bleDataTemp;
                        totalPacket++;
                        refreshText();

                        if (mData.contains("}")) {
                            Log.e("TAG", "data:" + mData);
                            Log.e("TAG", "totalLength:" + mData.length());
                            mData = "";
                            totalPacket = 0;
                        }
                    }
                });
    }

    private void refreshText() {
        tvByte.setText("已接收： " + mData.length() + "字节 " + totalPacket + "包");
        tvContent.setText(mData);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().clearCharacterCallback(bleDevice);

        if (BleManager.getInstance().isConnected(bleDevice)) {
            BleManager.getInstance().disconnect(bleDevice);
        }
    }

}